package com.globalwave.system.entity;

import java.util.Date;
import java.util.Set;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.globalwave.common.Convertor;
import com.globalwave.common.U;
import com.globalwave.common.exception.BusinessException;

public class SessionUser {

    final public static String SESSION_PK = "USER_SESSION_ID_07611239" ;
    final public static boolean IS_VALIDE_PRIVILEGE = false ;
    
    private User user ;
    private Set<Short> organization_ids ;
    private Set<Short> privilege_ids ;
    
    private Integer sale_date ;
    private String terminal_code ;
    
    private static ThreadLocal<SessionUser> tl=new ThreadLocal<SessionUser> ();//私有静态变量 
    public static SessionUser get(){
    	SessionUser su = tl.get() ;
    	if(su == null) {
    		throw new BusinessException("user.login.timeout") ;
    	}
        return su;
    }
    public static void set(SessionUser user){
        tl.set(user);
    } 

    public Set<Short> getOrganization_ids() {
        return organization_ids;
    }
    public void setOrganization_ids(Set<Short> organization_ids) {
        this.organization_ids = organization_ids;
    }
    public Set<Short> getPrivilege_ids() {
        return privilege_ids;
    }
    public void setPrivilege_ids(Set<Short> privilege_ids) {
        this.privilege_ids = privilege_ids;
    }
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
	
	public String getShop_code() {
		return getUser().getShop_code();
	}
	
	public String getLogin_id() {
   	    return getUser().getLogin_id() ;  
    }
   
	public Integer getSale_date() {
		return sale_date;
	}
	public void setSale_date(Date sale_date) {
		this.sale_date = Long.valueOf(U.date2int(sale_date)/1000000).intValue();
	}
	public String getTerminal_code() {
		return terminal_code;
	}
	public void setTerminal_code(String terminal_code) {
		this.terminal_code = terminal_code;
	}
	
    
    public String asXML() {
        
        return toXML().asXML() ;
    }

    public Element toXML() {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("root") ;
                
        try {
        	root.add(Convertor.object2Xml(user)) ;

        	Convertor.addProperty(root, "sale_date", sale_date == null?"":sale_date) ;
        	Convertor.addProperty(root, "terminal_code", terminal_code == null?"":terminal_code) ;
        	
            root.add(Convertor.list2Xml(privilege_ids,"privilege_ids")) ;
            root.add(Convertor.list2Xml(organization_ids,"organization_ids")) ;
        } catch (Exception e) {
            root.addElement("message").addText("Conver array page list to xml error!") ;
        }
        
        return root ;
    }
    
    
    public boolean hasPrivilege(short pid) {
        if (IS_VALIDE_PRIVILEGE) {
            return this.privilege_ids.contains(pid) ;            
        } else {
            return true ;
        }
    }
    
}
